
#include "samplers.fx"

#pragma warning( disable : 4715 )

struct VS_IN
{
	uint vertexId	:	SV_VertexId;
};

struct VS_OUT
{
	float3	position	:	POSITION;
	uint	id			:	ID;
};


struct GS_OUT
{
	float4	pos	:	SV_POSITION;
	float2	uv	:	COORDS;
	uint	id	:	ID;
};


Texture2DArray	texarrObjects;
float4x4 m_World;
float4x4 mViewProj;

static const float dist = 200.0f;
static const float height = 50.0f;
static const float size = 50.0f;

float3 quads_positions[4] = {	float3(dist, height, 0.0f), 
								float3(-dist, height, 0.0f),
								float3(0.0f, height, dist),
								float3(0.0f, height, -dist),
							};


VS_OUT VS(VS_IN vin)
{
	
	VS_OUT vout;
	vout.position = quads_positions[vin.vertexId];
	vout.id = vin.vertexId;
	return vout;
}


[maxvertexcount(4)]
void GS(point VS_OUT gin[1], inout TriangleStream<GS_OUT> gstream)
{
	float3 p = gin[0].position;
	p.xz = float2(0,0);

	float3 vLook = normalize(p - gin[0].position);
	float3 vUp   = float3(0,1,0);
	float3 vRight= normalize(cross(vUp, vLook));

	GS_OUT gout[4];

	gout[0].pos = float4(gin[0].position + size*vRight + size*vUp, 1.0f);
	gout[1].pos = float4(gin[0].position - size*vRight + size*vUp, 1.0f);
	gout[2].pos = float4(gin[0].position + size*vRight - size*vUp, 1.0f);
	gout[3].pos = float4(gin[0].position - size*vRight - size*vUp, 1.0f);

	gout[0].uv  = float2(0.0f, 0.0f);
	gout[1].uv  = float2(1.0f, 0.0f);
	gout[2].uv  = float2(0.0f, 1.0f);
	gout[3].uv  = float2(1.0f, 1.0f);

	gout[0].id = gin[0].id;
	gout[1].id = gin[0].id;
	gout[2].id = gin[0].id;
	gout[3].id = gin[0].id;

	for (int i=0; i<4; i++) {
		gout[i].pos = mul(gout[i].pos, mViewProj);
		gstream.Append(gout[i]);
	}
}


float4 PS(GS_OUT pin) : SV_TARGET
{
	float3 color = texarrObjects.Sample(s_linear, float3(pin.uv, pin.id)).xyz;
	return float4(color,1);
}



RasterizerState rs_Wireframe
{
	CullMode = Back;
	FillMode = Wireframe;
};


technique10 AlienVS
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_4_0,  VS()   ) );
		SetHullShader ( NULL );
		SetDomainShader( NULL );
		SetGeometryShader( CompileShader( gs_4_0, GS() ) );
		SetPixelShader( CompileShader( ps_4_0,   PS()   ) );
		
		SetRasterizerState(0);
		SetDepthStencilState(0, 0);
	}
}
